/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gw.reportbuilder.filter;

import gw.reportbuilder.column.Column;

/**
 *
 * @author Sostenes
 */
public enum Operator {

    BETWEEN(":column BETWEEN ':arg1' AND ':arg2'", "entre") {

        @Override
        public String getSQLClause(Column column, String... args) {

            if (args[0].trim().isEmpty() && args[1].trim().isEmpty()) {
                
                return "";
                
            }else if (args[0].trim().isEmpty()) {

                return column.getName() + " < '" + args[1] + "'";

            } else if (args[1].trim().isEmpty()) {

                return column.getName() + " > '" + args[0] + "'";
            } 

            return this.getClause().replace(":column", column.getName()).replace(":arg1", args[0]).replace(":arg2", args[1]);
        }
    },
    IN(":column IN (:args)", "igual a") {

        @Override
        public String getSQLClause(Column column, String... args) {
            
            if (args.length == 1 && args[0].trim().isEmpty() && column.getType().equals("java.sql.Date")) {
                return "";
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < args.length; i++) {

                if (i > 0) {
                    sb.append(", ");
                }
                sb.append("'").append(args[i]).append("'");

            }

            return this.getClause().replace(":column", column.getName()).replace(":args", sb.toString());
        }
    },
    NOT_IN(":column NOT IN (:args)", "diferente de") {

        @Override
        public String getSQLClause(Column column, String... args) {
            
            if (args.length == 1 && args[0].trim().isEmpty() && column.getType().equals("java.sql.Date")) {
                return "";
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < args.length; i++) {

                if (i > 0) {
                    sb.append(", ");
                }
                sb.append("'").append(args[i]).append("'");

            }

            return this.getClause().replace(":column", column.getName()).replace(":args", sb.toString());
        }
    };

    private Operator(String clause, String label) {
        this.clause = clause;
        this.label = label;
    }
    private String clause;
    private String label;

    public abstract String getSQLClause(Column column, String... args);

    String getClause() {
        return this.clause;
    }

    public String getLabel() {
        return this.label;
    }
}
